/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean;

 
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import model.entity.Problem;
import model.entity.Resource;
 
import org.omnifaces.util.Faces;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;
import service.InterfaceServiceLocal;
import service.ResourceServiceLocal;
import utils.QueryStr;

/**
 *
 * @author Administrator
 */
@Named(value = "menuMB")
@SessionScoped
public class MenuMB implements Serializable {

//    private TreeNode root;
//    private TreeNode selectedNode;
    private QueryStr querystring;
    private JSONObject menus = null;
    @Inject
    private shiroMB shiroMB;
    @EJB(beanName = "ResourceManager")
    private ResourceServiceLocal resourceManager;

    /**
     * Creates a new instance of TreeCheckboxBean
     */
    public MenuMB() {
    }

    public JSONObject getMenus() throws JSONException {
        if (menus == null) {

            JSONArray menuArray = new JSONArray();
            menus = new JSONObject();
            String context = Faces.getRequestBaseURL() + "faces/";

            List<Resource> list0 = resourceManager.getResourcesbyParentNodeId(0, 1, -1, true);

            for (int i = 0; i < list0.size(); i++) {

                Resource current = list0.get(i);
                List<Resource> list1 = resourceManager.getResourcesbyLoginname((String) shiroMB.getSubject().getPrincipal(), 1, current.getIdresource(), -1, true);
                if (list1.isEmpty()) {
                    continue;
                }
                JSONArray submenuArray = new JSONArray();
                JSONObject json = new JSONObject();
                json.put("menuid", current.getIdresource());
                //json.put("icon", "icon-sys");
                json.put("menuname", current.getNodename());

                for (int j = 0; j < list1.size(); j++) {
                    Resource sub = list1.get(j);
                    JSONObject subjson = new JSONObject();
                    subjson.put("menuid", sub.getIdresource());
                    //subjson.put("icon", "icon-sys");
                    subjson.put("menuname", sub.getNodename());
                    subjson.put("url", context + sub.getNodeUrl() + ".xhtml");
                    submenuArray.put(subjson);
                }
                json.put("menus", submenuArray);
                menuArray.put(json);

            }

            menus.put("menus", menuArray);

            System.out.println("menus=" + menus.toString());
        }
        return menus;
    }

//    public JSONArray createTree(List<Resources> list) throws JSONException {
//        String str = "";
//        String condition = "";
//        JSONArray menuArray = new JSONArray();
//        String context = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/faces/";
//        for (int i = 0; i < list.size(); i++) {
//            Resources current = list.get(i);
//            JSONArray submenuArray = new JSONArray();
//            JSONObject json = new JSONObject();
//            json.put("menuid", current.getId());
//            json.put("icon", "icon-sys");
//            json.put("menuname", current.getNode());
//            condition = querystring.addLogicalCondition("where", "parentNodeIndex", "=", current.getId() + "") + " and s.id in(select distinct t1.sourceID.id from RolePermissions t1,Userrole t2 where t1.permissionQuery = 'Y' and t2.userid.idsoldier = '" + ejbFacade.getShiro().getUsername() + "' and t1.roleID = t2.roleid )"
//                    + querystring.addLogicalCondition("and", "nodeDepth", ">", "0") + querystring.orderStr("px", "asc");
//            str = querystring.getQueryList() + condition;
//            List<Resources> list0 = ejbFacade.findAll(str);
//            for (int j = 0; j < list0.size(); j++) {
//                Resources sub = list0.get(j);
//                JSONObject subjson = new JSONObject();
//                subjson.put("menuid", sub.getId());
//                subjson.put("icon", "icon-sys");
//                subjson.put("menuname", sub.getNode());
//                subjson.put("url", context + sub.getNodeLink().substring(1) + ".xhtml");
//                submenuArray.put(subjson);
//            }
//            json.put("menus", submenuArray);
//            menuArray.put(json);
//        }
//        return menuArray;
//    }
//
//    public void setBool(boolean bool) {
//        this.bool = bool;
//    }
    protected void setMenus(JSONObject menus) {
        this.menus = menus;
    }
}
